package com.kakavr.emp.controller;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.beust.jcommander.internal.Maps;
import com.kakavr.base.controller.BaseController;
import com.kakavr.base.exception.ValidateParamException;
import com.kakavr.order.service.IPurchaseHistoryService;
import com.kakavr.result.MsgResult;
import com.kakavr.result.constant.MsgConstants;
import com.kakavr.shop.service.ITradeFlowService;
import com.kakavr.statistics.service.IStatisticsService;
import com.kakavr.user.service.IUserRechargeService;
import com.kakavr.user.service.IUserService;
import com.kakavr.util.DateTimeUtils;
import com.kakavr.util.SessionUtils;

/**
 * 店铺数据统计
 * @author gang.yang
 *
 */
@RestController
@RequestMapping("shopManage")
public class ShopManageController extends BaseController {
	@Resource
	private IPurchaseHistoryService purchaseHistoryService;
	@Resource
	private ITradeFlowService tradeFlowService;
	@Resource
	private IUserRechargeService userRechargeService;
	@Resource
	private IUserService userService;
	@Resource
	private IStatisticsService statisticsService;

	/**
	 * 交易流水记录
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectTradeFlowList", method = RequestMethod.GET)
	public Map<String, Object> selectTradeFlowList(HttpServletRequest request){
		Map<String, Object> map = this.initPaginationParams(request);
		map.put("storeId", SessionUtils.getShopId());
		map.put("begin", request.getParameter("begin"));
		map.put("end", request.getParameter("end"));
		map.put("splitId", request.getParameter("splitId"));
		map.put("dateFlag", request.getParameter("dateFlag"));
		return MsgResult.okResultData(tradeFlowService.selectTradeFlowList(map));
	}
	
	/**
	 * 充值记录
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectRechargeList", method = RequestMethod.GET)
	public Map<String, Object> selectRechargeList(HttpServletRequest request){
		Map<String, Object> map = this.initPaginationParams(request);
		map.put("storeId", SessionUtils.getShopId());
		map.put("begin", request.getParameter("begin"));
		map.put("end", request.getParameter("end"));
		return MsgResult.okResultData(userRechargeService.selectRechargeByPage(map));
	}
	
	/**
	 * 消费记录
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectPurchaseList", method = RequestMethod.GET)
	public Map<String, Object> selectPurchaseList(HttpServletRequest request){
		Map<String, Object> map = this.initPaginationParams(request);
		map.put("storeId", SessionUtils.getShopId());
		map.put("begin", request.getParameter("begin"));
		map.put("end", request.getParameter("end"));
		return MsgResult.okResultData(purchaseHistoryService
				.selectPurchaseListByPage(map));
	}
	
	/**
	 * 消费详情
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectPurchaseInfo", method = RequestMethod.GET)
	public Map<String, Object> selectPurchaseInfo(HttpServletRequest request) {
		String purchaseId = request.getParameter("purchaseId");
		if (StringUtils.isBlank(purchaseId)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "消费id");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("purchaseId", purchaseId);
		return MsgResult.okResultData(purchaseHistoryService
				.selectPurchaseInfo(map));
	}
	
	
	/**
	 * 用户列表
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectUserList", method = RequestMethod.GET)
	public Map<String, Object> selectUserList(HttpServletRequest request){
		Map<String, Object> map = this.initPaginationParams(request);
		map.put("storeId", SessionUtils.getShopId());
		map.put("name", request.getParameter("name"));
		return MsgResult.okResultData(userService.selectUserList(map));
	}
	
	/**
	 * 销售报表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "sale", method = RequestMethod.GET)
	public Map<String, Object> sale(HttpServletRequest request){
		String type = request.getParameter("type");
		String begin = request.getParameter("begin");
		String end = request.getParameter("end");
		Map<String, Object> map = this.initPaginationParams(request);
		if (StringUtils.isNotBlank(begin)) {
			map.put("begin", begin);
		}
		if (StringUtils.isNotBlank(end)) {
			map.put("end", end);
		}
		map.put("type", type);
		map.put("storeId", SessionUtils.getShopId());
		return MsgResult.okResultData(statisticsService.statisSale(map));
	}
	
	/**
	 * 统计游戏玩的次数
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectGameCountList", method = RequestMethod.GET)
	public Map<String, Object> selectGameCountList(HttpServletRequest request){
		String type = request.getParameter("type");
		String begin = request.getParameter("begin");
		String end = request.getParameter("end");
		Map<String, Object> map = this.initPaginationParams(request);
		Date time = DateTime.now().toDate();
		if(StringUtils.isNotBlank(begin)){
			map.put("begin", begin);
		}
		if(StringUtils.isNotBlank(end)){
			map.put("end", end);
		}
		map.put("storeId", SessionUtils.getShopId());
		if("week".equals(type)){
			map.put("begin", DateTimeUtils.getThisWeekMonday(time));
		}
		map.put("time", time);
		map.put("type", type);
		return MsgResult.okResultData(purchaseHistoryService
				.selectGameHistoryList(map));
	}
}
